問題描述
django.db.utils.OperationalError:外鍵不匹配 ‑ “project_projectpage”引用“auth_user” (django.db.utils.OperationalError: foreign key mismatch ‑ "project_projectpage" referencing "auth_user")
這個問題首先想說什麼?
下面是我的model.py
class ProjectPage(Page):
"""
A Project Page
We access the People object with an inline panel that references the
ParentalKey's related_name in ProjectPeopleRelationship. More docs:
http://docs.wagtail.io/en/latest/topics/pages.html#inline‑models
"""
introduction = models.TextField(
help_text='Text to describe the page',
blank=True)
image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
help_text='Landscape mode only; horizontal width between 1000px and 3000px.'
)
body = StreamField(
BaseStreamBlock(), verbose_name="Page body", blank=True
)
subtitle = models.CharField(blank=True, max_length=255)
tags = ClusterTaggableManager(through=ProjectPageTag, blank=True)
date_published = models.DateField(
"Date article published", blank=True, null=True
)
#email = models.CharField(max_length=255, blank=True, null=True)
email = models.ForeignKey(User, on_delete=models.PROTECT, to_field='email', null=True)
content_panels = Page.content_panels + [
FieldPanel('subtitle', classname="full"),
FieldPanel('introduction', classname="full"),
ImageChooserPanel('image'),
StreamFieldPanel('body'),
FieldPanel('date_published'),
InlinePanel(
'project_person_relationship', label="Author(s)",
panels=None, min_num=1),
FieldPanel('email'),
FieldPanel('tags'),
FieldPanel('added_by', classname="full"),
FieldPanel('updated_by', classname="full"),
]
search_fields = Page.search_fields + [
index.SearchField('body'),
]
added_by = models.ForeignKey(People,related_name="project_added_by", null=True, blank=True, on_delete=models.SET_NULL)
updated_by = models.ForeignKey(People,related_name="project_updated_by", null=True, blank=True, on_delete=models.SET_NULL)
created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)
updated_at = models.DateTimeField(auto_now=True, null=True, blank=True)
上述問題的解決方案是什麼,如果您想了解更多詳細信息,請告訴我對於這個問題!!!!
我不知道,但我認為這裡的電子郵件 ID 或其他地方有問題,因為我添加了電子郵件 ID,之後可能會出現這個問題。
參考解法
方法 1:
The declaration for email
on Django's User
model is:
email = models.EmailField(_('email address'), blank=True)
This part of the Django documentation says that, if you link to any field besides the default key (such as how you're trying to link to email
), then that field would have to be declared with unique=True
, but email
isn't. The problem with how you're trying to link is that there is no index on the User
model's email
field because it isn't declared as unique.
It would be more standard to link to the Django user model like this (by default, the link is made on the user model's default key, the id):
from django.contrib.auth import get_user_model
user = models.ForeignKey(get_user_model(), on_delete=models.PROTECT, null=True)
With the above declaration, in your template you would get the linked user's email by {{ page.user.email }}
, but I wonder about the architecture of linking a user to a page. Remember that Wagtail has a permissions structure that you can use to assign permissions to groups of users for specific page types/models.
(by Chirag Kanzariya、Dan Swain)